package zmee.bid.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
 * @author lijiawang
 * @Date 2022/12/1 14:44
 * @Description:招标立项申请单提交
 */
public class SrcDemandOperation extends AbstractOperationServicePlugIn {

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                String key = this.getOperateKey();
                if ("submit".equals(key)) {
                    ExtendedDataEntity[] dataEntities = this.getDataEntities();
                    for (int i = 0; i < dataEntities.length; i++) {
                        DynamicObject dataEntity = dataEntities[i].getDataEntity();
                        DynamicObject dynamic = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDataEntityType().getName());
                        String control = dynamic.getString("control");
                        if (control != null && "1".equals(control)) {
                            continue;
                        }
                        DynamicObjectCollection entryentity = dynamic.getDynamicObjectCollection("entryentity");
                        String group = dynamic.getString("group.number");
                        Map<String, BigDecimal> maps;
                        switch (group) {
                            case "10201":
                            case "10301":
                            case "10302":
                            case "10303":
                            case "10304":
                            case "10305":
                            case "10401":
                                maps = entyNumTaxamount(entryentity, "amount");
                                break;
                            default:
                                maps = entyNumTaxamount(entryentity, "taxamount");
                                break;
                        }
                        if (control != null && "3".equals(control)) {
                            for (String keyMap : maps.keySet()) {
                                BigDecimal bigDecimal = maps.get(keyMap);
                                if (bigDecimal.compareTo(new BigDecimal("0")) == -1) {
                                    this.addErrorMessage(dataEntities[i], "预算余额不足、不允许提交！");
                                }
                            }

                        } else if ("2".equals(control)) {
                            for (String keyMap : maps.keySet()) {
                                BigDecimal bigDecimal = maps.get(keyMap);
                                if (bigDecimal.compareTo(new BigDecimal("0")) == -1) {
                                    this.addMessage(dataEntities[i], "预算金额不足，项目预算金额差额" + bigDecimal);
                                }
                            }

                        }
                    }
                }
            }
        });
    }

    //根据预算项计算在分录中所有的价税合计/未税金额集合
    private Map<String, BigDecimal> entyNumTaxamount(DynamicObjectCollection entryentity, String txt) {
        Map<String, BigDecimal> maps = new HashMap<>();
        for (int i = 0; i < entryentity.size(); i++) {
            DynamicObject entr = entryentity.get(i);
            BigDecimal taxamount = entr.getBigDecimal(txt);//价税合计
            DynamicObject approvalbudget = entr.getDynamicObject("approvalbudget");
            if (approvalbudget != null) {
                String id = approvalbudget.getPkValue().toString();
                BigDecimal big = maps.get(id);
                if (big != null) {
                    BigDecimal budge = big.add(taxamount);
                    maps.put(id, budge);
                } else {
                    BigDecimal balance = entr.getBigDecimal(txt);
                    maps.put(id, balance);
                }
            }
        }
        return maps;
    }
}
